<?php
    //合成分享图片APP
    function shareApp(Request $request){
        $share_code = $request->input()["share_code"];
        if(empty($share_code)){
            return return_json_data(500, '分享码不存在');
        }
        $rs = DB::select('select id,name from `users` where share_code="'.$share_code.'" limit 1');
        if(empty($rs)){
            return return_json_data(500, '分享码不存在');
        }        
        $uid = $rs[0]->id;
        $name = $rs[0]->name;
        if(mb_strlen($name)>5){
            $name = mb_substr($name,0,3).'...';
        }
        $des = $name.'的邀请码';
        #分享二维码
        $dir = $_SERVER['DOCUMENT_ROOT'] . '/share/';
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }     
        if (php_uname('s') == "Windows NT") {
            $dir = str_replace("\\", "/", $dir);
        }
        
        $data = array();
        $data['img_url'] = '/share/' . 'share_app/'.$uid . '.jpg';
        $file_url = $dir . 'share_app/'.$uid . '.jpg';
        if(file_exists($file_url)){
            return return_json_data(Response::OK, '', $data);
        }
        
        $file_url = $dir . 'qrcode/'.$uid . '.jpg';
        if(!file_exists($file_url)){
            $text = 'http://gutenberg.findata.vip/user/register?share_code='.$share_code;
            $qrCode = new QRcode();//创建生成二维码对象
            $qrCode::png($text, $file_url, QR_ECLEVEL_L, 8, 1);        
        }
        
        $imgs = $file_url;//照片
        $target = $dir.'share_app.jpg';//背景图片
        $target_img = Imagecreatefromjpeg($target);
        $source= array();
        $source['source'] = imagecreatefrompng($imgs);
        $source['size'] = getimagesize($imgs);
        imagecopy ($target_img,$source['source'],162,440,0,0,$source['size'][0],$source['size'][1]);
        ImageTTFText($target_img, 16, 0, 208, 387, 0, $dir.'MSYH.TTC', $des);
        ImageTTFText($target_img, 16, 0, 212, 833, 0, $dir.'MSYH.TTC', $des);
        ImageTTFText($target_img, 32, 0, 215, 928, 0, $dir.'MSYH.TTC', $share_code);
        Imagejpeg($target_img,$dir.'share_app/'.$uid.'.jpg');
        
        $file_url = $dir . 'share_app/'.$uid . '.jpg';    
        return return_json_data(Response::OK, '', $data);
    }

